package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NMaterielDeviceBo;
import com.ruoyi.zhgd.domain.vo.NMaterielDeviceVo;
import com.ruoyi.zhgd.domain.NMaterielDevice;
import com.ruoyi.zhgd.mapper.NMaterielDeviceMapper;
import com.ruoyi.zhgd.service.INMaterielDeviceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 地磅Service业务层处理
 *
 * @author zyb
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NMaterielDeviceServiceImpl implements INMaterielDeviceService {

    private final NMaterielDeviceMapper baseMapper;

    /**
     * 查询地磅
     */
    @Override
    public NMaterielDeviceVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询地磅列表
     */
    @Override
    public TableDataInfo<NMaterielDeviceVo> queryPageList(NMaterielDeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NMaterielDevice> lqw = buildQueryWrapper(bo);
        Page<NMaterielDeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询地磅列表
     */
    @Override
    public List<NMaterielDeviceVo> queryList(NMaterielDeviceBo bo) {
        LambdaQueryWrapper<NMaterielDevice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NMaterielDevice> buildQueryWrapper(NMaterielDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NMaterielDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NMaterielDevice::getBaseCode, bo.getBaseCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), NMaterielDevice::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), NMaterielDevice::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getWeighbridgeManufacturer()), NMaterielDevice::getWeighbridgeManufacturer, bo.getWeighbridgeManufacturer());
        lqw.eq(StringUtils.isNotBlank(bo.getWeighbridgeContacts()), NMaterielDevice::getWeighbridgeContacts, bo.getWeighbridgeContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getWeighbridgeModel()), NMaterielDevice::getWeighbridgeModel, bo.getWeighbridgeModel());
        lqw.eq(StringUtils.isNotBlank(bo.getWeighbridgePhone()), NMaterielDevice::getWeighbridgePhone, bo.getWeighbridgePhone());
        lqw.eq(StringUtils.isNotBlank(bo.getInstrumentManufacturer()), NMaterielDevice::getInstrumentManufacturer, bo.getInstrumentManufacturer());
        lqw.eq(StringUtils.isNotBlank(bo.getInstrumentContacts()), NMaterielDevice::getInstrumentContacts, bo.getInstrumentContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getInstrumentModel()), NMaterielDevice::getInstrumentModel, bo.getInstrumentModel());
        lqw.eq(StringUtils.isNotBlank(bo.getInstrumentPhone()), NMaterielDevice::getInstrumentPhone, bo.getInstrumentPhone());
        lqw.eq(bo.getCreator() != null, NMaterielDevice::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, NMaterielDevice::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, NMaterielDevice::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, NMaterielDevice::getUpdateDate, bo.getUpdateDate());
        return lqw;
    }

    /**
     * 新增地磅
     */
    @Override
    public Boolean insertByBo(NMaterielDevice bo) {
        NMaterielDevice add = BeanUtil.toBean(bo, NMaterielDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改地磅
     */
    @Override
    public Boolean updateByBo(NMaterielDevice bo) {
        NMaterielDevice update = BeanUtil.toBean(bo, NMaterielDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NMaterielDevice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除地磅
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PageUtils materielDeviceList(Page page, Map<String, Object> params) {
        if(params.get("id") == null){
            params.put("id", LoginHelper.getDeptId().toString());
        }
        List<NMaterielDeviceVo> maps = baseMapper.materielDeviceList(page, params);
        return new PageUtils(page.setRecords(maps));
    }

    @Override
    public TableDataInfo<Map> listAll(NMaterielDeviceBo bo) {
        List<Map> data = baseMapper.listAll(bo);
        return TableDataInfo.build(data);
    }

    @Override
    public List<NMaterielDeviceVo> codeList(NMaterielDeviceBo bo) {
        if (ObjectUtils.isEmpty(bo.getType())) {
            throw new ServiceException("设备类型不能为空");
        }
        return baseMapper.codeList(bo);
    }
}
